define([
    'esri/units',
    'esri/geometry/Extent',
    'esri/config',
    'esri/dijit/Basemap',
    'esri/dijit/BasemapLayer',
    /*'esri/urlUtils',*/
    'esri/tasks/GeometryService',
    'esri/layers/ImageParameters',
    'esri/dijit/Popup',
    'esri/symbols/SimpleFillSymbol',
    'esri/symbols/SimpleLineSymbol',
    'esri/Color',
    'dojo/dom-construct',
    'dojo/i18n!./nls/main',
    'dojo/topic',
    'dojo/sniff'
], function (units, Extent, esriConfig, /*urlUtils,*/ Basemap, BasemapLayer, GeometryService, ImageParameters, Popup,
             SimpleFillSymbol, SimpleLineSymbol, Color, domConstruct, i18n, topic, has) {

    // url to your proxy page, must be on same machine hosting you app. See proxy folder for readme.
    // esriConfig.defaults.io.proxyUrl = urls.proxy_url;
    // console.log("proxy url =>", urls.proxy_url);
    // esriConfig.defaults.io.alwaysUseProxy = false;

    // url to your geometry server.
    esriConfig.defaults.geometryService = new GeometryService(urls.onemap_url + '/arcgis/rest/services/Utilities/Geometry/GeometryServer?token='+ GLOBAL_TOKEN);
    //turn off CORS detection.
    esriConfig.defaults.io.corsDetection = false;

    //image parameters for dynamic services, set to png32 for higher quality exports.
    var imageParameters = new ImageParameters();
    imageParameters.format = 'png8';
    function buildImageParameters(config) {
        config = config || {};
        var ip = new ImageParameters();
        //image parameters for dynamic services, set to png32 for higher quality exports
        ip.format = 'png32';
        for (var key in config) {
            if (config.hasOwnProperty(key)) {
                ip[key] = config[key];
            }
        }
        return ip;
    }

    return {
        // used for debugging your app
        isDebug: true,
        //default mapClick mode, mapClickMode lets widgets know what mode the map is in to avoid multipult map click actions from taking place (ie identify while drawing).
        defaultMapClickMode: '',

        // map options, passed to map constructor. see: https://developers.arcgis.com/javascript/jsapi/map-amd.html#map1
        mapOptions: {
            basemap: new Basemap({
                id: 'baseview',
                layers: [new BasemapLayer({
                    url: urls.onemap_url + urls.gis_basemap_url+'?token='+ GLOBAL_TOKEN
                })]
            }),
            extent: new Extent({
                xmin: -15200,
                ymin: -6700,
                xmax: -2000,
                ymax: 1550,
                spatialReference: {wkt: 'PROJCS["shanghaicity",GEOGCS["GCS_Beijing_1954",DATUM["D_Beijing_1954",SPHEROID["Krasovsky_1940",6378245.0,298.3]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",-3457147.81],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",121.2751921],PARAMETER["Scale_Factor",1.0],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]'}
            }),
            logo: false,
            sliderStyle: 'small',
            infoWindow: new Popup({fillSymbol: new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([255, 0, 0]), 2), new Color([255, 255, 0, 0.25]))}, domConstruct.create("div"))
        },

        panes: {},
        collapseButtonsPane: 'center', //center or outer

        layout: {},

        // operationalLayers: Array of Layers to load on top of the basemap: valid 'type' options: 'dynamic', 'tiled', 'feature'.
        // The 'options' object is passed as the layers options for constructor. Title will be used in the legend only. id's must be unique and have no spaces.
        // 3 'mode' options: MODE_SNAPSHOT = 0, MODE_ONDEMAND = 1, MODE_SELECTION = 2
        operationalLayers: [
            {
                type: 'dynamic',
                url: urls.onemap_url + urls.gis_kxgh_url+'?token='+ GLOBAL_TOKEN,
                title: '控详规划',
                options: {
                    id: 'kxgh',
                    visible: false
                },
                layerControlLayerInfos: {
                    swipe: true,
                    expanded: false
                }
            },
            {
                type: 'dynamic',
                url: urls.onemap_url + urls.gis_zxgh_url+'?token='+ GLOBAL_TOKEN,
                title: '专项规划',
                options: {
                    id: 'zxgh',
                    visible: true
                },
                layerControlLayerInfos: {
                    swipe: true,
                    expanded: false
                }
            },
            {
                type: 'dynamic',
                url: urls.onemap_url + urls.gis_fdsj_url+'?token='+ GLOBAL_TOKEN,
                title: '房地数据',
                options: {
                    id: 'fdsj',
                    visible: false
                },
                layerControlLayerInfos: {
                    swipe: true,
                    expanded: false
                }
            },
            {
                type: 'dynamic',
                url: urls.onemap_url + urls.gis_hxsj_url+'?token='+ GLOBAL_TOKEN,
                title: '红线数据',
                options: {
                    id: 'hxsj',
                    visible: false
                },
                layerControlLayerInfos: {
                    swipe: true,
                    expanded: false
                }
            },
            {
                type: 'dynamic',
                url: urls.onemap_url + urls.gis_cnstreet_url+'?token='+ GLOBAL_TOKEN,
                title: '长宁街道镇',
                options: {
                    id: 'cn_street',
                    opacity: 1.0,
                    visible: false,
                    imageParameters: imageParameters
                },
                layerControlLayerInfos: {
                    swipe: true,
                    expanded: false
                }
            },
            {
                type: 'dynamic',
                url: urls.onemap_url + urls.gis_boundary_url+'?token='+ GLOBAL_TOKEN,
                title: '长宁区界',
                options: {
                    id: 'boundary',
                    opacity: 1.0,
                    visible: true,
                    imageParameters: imageParameters
                },
                layerControlLayerInfos: {
                    swipe: true,
                    expanded: false
                }
            }
        ],

        // set include:true to load. For titlePane type set position the the desired order in the sidebar
        widgets: {
            placeAt: 'right',
            growler: {
                include: true,
                id: 'growler',
                type: 'domNode',
                path: 'gis/dijit/Growler',
                srcNodeRef: 'growlerDijit',
                options: {}
            },
            basemaps: {
                include: true,
                id: 'basemaps',
                type: 'domNode',
                path: 'gis/dijit/Basemaps',
                srcNodeRef: 'basemapsDijit',
                options: 'config/basemaps'
            },
            mapInfo: {
                include: true,
                id: 'mapInfo',
                type: 'domNode',
                path: 'gis/dijit/MapInfo',
                srcNodeRef: 'mapInfoDijit',
                options: {
                    map: true,
                    showScale: true
                }
            },
            overviewMap: {
                include: true,
                id: 'overviewMap',
                type: 'map',
                path: 'esri/dijit/OverviewMap',
                options: {
                    map: true,
                    attachTo: 'bottom-right',
                    color: '#000000',
                    height: 250,
                    width: 400,
                    opacity: 0.30,
                    visible: false
                }
            },
            layerControl: {
                include: true,
                id: 'layerControl',
                type: 'domNode',
                path: 'gis/dijit/LayerControl',
                srcNodeRef: 'maplayerSrcNodeRef',
                options: {
                    map: true,
                    topicID: 'layerControlWidget',
                    layerControlLayerInfos: true,
                    separated: true,
                    vectorReorder: true,
                    overlayReorder: true
                }
            },
            homeButton: {
                include: true,
                id: 'homeButton',
                type: 'domNode',
                path: 'esri/dijit/HomeButton',
                srcNodeRef: 'homeButton',
                options: {
                    map: true,
                    extent: new Extent({
                        xmin: -15200,
                        ymin: -6700,
                        xmax: -2000,
                        ymax: 1550,
                        spatialReference: {wkt: 'PROJCS["shanghaicity",GEOGCS["GCS_Beijing_1954",DATUM["D_Beijing_1954",SPHEROID["Krasovsky_1940",6378245.0,298.3]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",-3457147.81],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",121.2751921],PARAMETER["Scale_Factor",1.0],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]'}
                    })
                }
            },
            identifyButton: {
                include: true,
                id: 'identifyButton',
                type: 'domNode',
                path: 'gis/dijit/IdentifyButton',
                srcNodeRef: 'identifyButton',
                options: {
                    map: true
                }
            },
            findButton: {
                include: true,
                id: 'findButton',
                type: 'domNode',
                path: 'gis/dijit/FindButton',
                srcNodeRef: 'findButton',
                options: {
                    map: true
                }
            },
            analyzeButton: {
                include: true,
                id: 'analyzeButton',
                type: 'domNode',
                path: 'gis/dijit/AnalyzeButton',
                srcNodeRef: 'analyzeButton',
                options: {
                    map: true
                }
            },
            measureButton: {
                include: true,
                id: 'measureButton',
                type: 'domNode',
                path: 'gis/dijit/MeasureButton',
                srcNodeRef: 'measureButton',
                options: {
                    map: true
                }
            },
            addressButton: {
                include: true,
                id: 'dzLocateButton',
                type: 'domNode',
                path: 'gis/dijit/AddressButton',
                srcNodeRef: 'addressButton',
                options: {
                    map: true
                }
            },
            refreshButton: {
                include: true,
                id: 'refreshButton',
                type: 'domNode',
                path: 'gis/dijit/RefreshButton',
                srcNodeRef: 'refreshButton',
                options: {
                    map: true
                }
            },
            identify: {
                include: true,
                id: 'identify',
                type: 'invisible',
                path: 'gis/dijit/Identify',
                title: i18n.viewer.widgets.identify,
                iconClass: 'fa-info-circle',
                open: false,
                preload: true,
                position: 3,
                options: 'config/identify'
            },
            find: {
                include: true,
                id: 'find',
                type: 'domNode',
                path: 'gis/dijit/Find',
                title: i18n.viewer.widgets.find,
                srcNodeRef: 'findSrcNodeRef',
                options: {
                    map: true,
                    queries: [
                        {
                            url: urls.onemap_url + urls.gis_zxgh_url+'?token='+ GLOBAL_TOKEN,
                            layerIds: [0],
                            searchFields: ['xmmc', 'jsdw', 'gcxkz', 'zzdah', 'jgdah'],
                            gridColumns: [
                                {field: 'objid', label: '编号', resizable: false, width: 50, visible: true},
                                {field: 'ename', label: '工程许可证号', visible: true}
                            ],
                            sort: [
                                {
                                    attribute: 'ename',
                                    descending: false
                                }
                            ]
                        }
                    ],
                    selectionMode: 'extended'
                }
            },
            analyze: {
                include: true,
                id: 'analyze',
                type: 'domNode',
                path: 'gis/dijit/Analyze',
                title: i18n.viewer.widgets.analyze,
                srcNodeRef: 'analyzeSrcNodeRef',
                options: {
                    map: true
                }
            },
            measure: {
                include: true,
                id: 'measure',
                type: 'domNode',
                path: 'gis/dijit/Measure',
                title: i18n.viewer.widgets.measure,
                srcNodeRef: 'measureSrcNodeRef',
                options: {
                    map: true,
                    mapClickMode: true
                }
            },
            address: {
                include: true,
                id: 'address',
                type: 'domNode',
                path: 'gis/dijit/Address',
                title: i18n.viewer.widgets.address,
                srcNodeRef: 'addressSrcNodeRef',
                options: {
                    map: true
                }
            }
        }
    };
});
